package com.hcinfos.gsoo.ui;

import java.util.ArrayList;
import java.util.List;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.json.JSONObject;

import com.hcinfos.gsoo.R;
import com.hcinfos.gsoo.adapter.GiftAdapter;
import com.hcinfos.gsoo.config.Urls;
import com.hcinfos.gsoo.enity.Gift;
import com.hcinfos.gsoo.util.LogUtil;
import com.hcinfos.gsoo.util.UtilHttp;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;

public class GiftActivity extends Activity implements OnClickListener {

	private Intent intent;

	private ImageView back_iv;
	private ListView listView;
	private GiftAdapter adapter;
	private List<Gift> list;
	private ObjectMapper mapper;

	public final int LIST_SUCCESS = 0x2210;
	public final int LIST_FAIL = LIST_SUCCESS + 1;

	private ProgressBar progress;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gift);

		progress = (ProgressBar) findViewById(R.id.progress);
		mapper = new ObjectMapper();
		back_iv = (ImageView) findViewById(R.id.back_iv);
		back_iv.setOnClickListener(this);
		listView = (ListView) findViewById(R.id.listView);

		list = new ArrayList<Gift>();
		adapter = new GiftAdapter(this, list);
		listView.setAdapter(adapter);
		obtainData();

		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {

				Intent intent = new Intent(GiftActivity.this,
						GiftConfirActivity.class);
				intent.putExtra("gift", list.get(position));
				startActivity(intent);

			}
		});
	}

	private List<Gift> gifts;

	private void obtainData() {
		listView.setVisibility(View.GONE);
		progress.setVisibility(View.VISIBLE);
		HttpUtils http = UtilHttp.getHttpUtils(this);
		RequestParams params = new RequestParams();
		params.addBodyParameter("page", 1 + "");
		params.addBodyParameter("rows", 100 + "");

		http.send(HttpRequest.HttpMethod.POST, Urls.callnew_giftList, params,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						handler.sendEmptyMessage(LIST_FAIL);
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {

						String result = arg0.result;
						try {

							JSONObject obj = new JSONObject(result);
							int state = obj.getInt("state");
							if (state == 200) {
								String data = obj.getString("data");
								gifts = mapper.readValue(data,
										new TypeReference<List<Gift>>() {
										});
								handler.sendEmptyMessage(LIST_SUCCESS);
							} else {
								handler.sendEmptyMessage(LIST_FAIL);
							}
						} catch (Exception e) {
							LogUtil.e("wxf", e.toString());
							handler.sendEmptyMessage(LIST_FAIL);
						}
					}

				});

	}

	Handler handler = new Handler() {

		public void handleMessage(android.os.Message msg) {

			listView.setVisibility(View.VISIBLE);
			progress.setVisibility(View.GONE);
			switch (msg.what) {
			case LIST_SUCCESS:
				if (gifts != null && gifts.size() > 0) {
					list.addAll(gifts);
					adapter.setDataChange(gifts);
				}
				break;
			case LIST_FAIL:

				break;
			}
		};
	};

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.back_iv:
			finish();
			break;
		}
	}
}
